
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>The new C/C++ parser &#8212; Universal Ctags 0.3.0 documentation</title>
    <link rel="stylesheet" href="_static/classic.css" type="text/css" />
    <link rel="stylesheet" href="_static/pygments.css" type="text/css" />
    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    './',
        VERSION:     '0.3.0',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true,
        SOURCELINK_SUFFIX: '.txt'
      };
    </script>
    <script type="text/javascript" src="_static/jquery.js"></script>
    <script type="text/javascript" src="_static/underscore.js"></script>
    <script type="text/javascript" src="_static/doctools.js"></script>
    <link rel="index" title="Index" href="genindex.html" />
    <link rel="search" title="Search" href="search.html" />
    <link rel="next" title="The Vim parser" href="parser-vim.html" />
    <link rel="prev" title="Parsers" href="parsers.html" /> 
  </head>
  <body>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="parser-vim.html" title="The Vim parser"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="parsers.html" title="Parsers"
             accesskey="P">previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">Universal Ctags 0.3.0 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="parsers.html" accesskey="U">Parsers</a> &#187;</li> 
      </ul>
    </div>  

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="the-new-c-c-parser">
<span id="cxx"></span><h1>The new C/C++ parser<a class="headerlink" href="#the-new-c-c-parser" title="Permalink to this headline">¶</a></h1>
<table class="docutils field-list" frame="void" rules="none">
<col class="field-name" />
<col class="field-body" />
<tbody valign="top">
<tr class="field-odd field"><th class="field-name">Maintainer:</th><td class="field-body">Szymon Tomasz Stefanek &lt;<a class="reference external" href="mailto:s&#46;stefanek&#37;&#52;&#48;gmail&#46;com">s<span>&#46;</span>stefanek<span>&#64;</span>gmail<span>&#46;</span>com</a>&gt;</td>
</tr>
</tbody>
</table>
<div class="section" id="introduction">
<h2>Introduction<a class="headerlink" href="#introduction" title="Permalink to this headline">¶</a></h2>
<p>The C++ language has strongly evolved since the old C/C++ parser was
written. The old parser was struggling with some of the new features
of the language and has shown signs of reaching its limits. For this
reason in February/March 2016 the C/C++ parser was rewritten from
scratch.</p>
<p>In the first release several outstanding bugs were fixed and some new
features were added. Among them:</p>
<ul class="simple">
<li>Tagging of “using namespace” declarations</li>
<li>Tagging of function parameters</li>
<li>Extraction of function parameter types</li>
<li>Tagging of anonymous structures/unions/classes/enums</li>
<li>Support for C++11 lambdas (as anonymous functions)</li>
<li>Support for function-level scopes (for local variables and parameters)</li>
<li>Extraction of local variables which include calls to constructors</li>
<li>Extraction of local variables from within the for(), while(), if()
and switch() parentheses.</li>
<li>Support for function prototypes/declarations with trailing return type</li>
</ul>
<p>At the time of writing (March 2016) more features are planned.</p>
</div>
<div class="section" id="notable-new-features">
<h2>Notable New Features<a class="headerlink" href="#notable-new-features" title="Permalink to this headline">¶</a></h2>
<p>Some of the notable new features are described below.</p>
<div class="section" id="properties">
<h3>Properties<a class="headerlink" href="#properties" title="Permalink to this headline">¶</a></h3>
<p>Several properties of functions and variables can be extracted
and placed in a new field called <code class="docutils literal"><span class="pre">properties</span></code>.
The syntax to enable it is:</p>
<div class="highlight-console"><div class="highlight"><pre><span></span><span class="gp">$</span> ctags ... --fields-c++<span class="o">=</span>+<span class="o">{</span>properties<span class="o">}</span> ...
</pre></div>
</div>
<p>At the time of writing the following properties are reported:</p>
<ul class="simple">
<li><code class="docutils literal"><span class="pre">virtual</span></code>: a function is marked as virtual</li>
<li><code class="docutils literal"><span class="pre">static</span></code>: a function/variable is marked as static</li>
<li><code class="docutils literal"><span class="pre">inline</span></code>: a function implementation is marked as inline</li>
<li><code class="docutils literal"><span class="pre">explicit</span></code>: a function is marked as explicit</li>
<li><code class="docutils literal"><span class="pre">extern</span></code>: a function/variable is marked as extern</li>
<li><code class="docutils literal"><span class="pre">const</span></code>: a function is marked as const</li>
<li><code class="docutils literal"><span class="pre">pure</span></code>: a virtual function is pure (i.e = 0)</li>
<li><code class="docutils literal"><span class="pre">override</span></code>: a function is marked as override</li>
<li><code class="docutils literal"><span class="pre">default</span></code>: a function is marked as default</li>
<li><code class="docutils literal"><span class="pre">final</span></code>: a function is marked as final</li>
<li><code class="docutils literal"><span class="pre">delete</span></code>: a function is marked as delete</li>
<li><code class="docutils literal"><span class="pre">mutable</span></code>: a variable is marked as mutable</li>
<li><code class="docutils literal"><span class="pre">volatile</span></code>: a function is marked as volatile</li>
<li><code class="docutils literal"><span class="pre">specialization</span></code>: a function is a template specialization</li>
<li><code class="docutils literal"><span class="pre">scopespecialization</span></code>: template specialization of scope <code class="docutils literal"><span class="pre">a&lt;x&gt;::b()</span></code></li>
<li><code class="docutils literal"><span class="pre">deprecated</span></code>: a function is marked as deprecated via <code class="docutils literal"><span class="pre">__attribute__</span></code></li>
<li><code class="docutils literal"><span class="pre">scopedenum</span></code>: a scoped enumeration (C++11)</li>
</ul>
</div>
<div class="section" id="preprocessor-macros">
<h3>Preprocessor macros<a class="headerlink" href="#preprocessor-macros" title="Permalink to this headline">¶</a></h3>
<p>The new parser supports the definition of real preprocessor macros
via the <code class="docutils literal"><span class="pre">-D</span></code> option. All types of macros are supported,
including the ones with parameters and variable arguments.
Stringification, token pasting and recursive macro expansion are also supported.</p>
<p>Option <code class="docutils literal"><span class="pre">-I</span></code> is now simply a backward-compatible syntax to define a
macro with no replacement.</p>
<p>The syntax is similar to the corresponding gcc <code class="docutils literal"><span class="pre">-D</span></code> option.</p>
<p>Some examples follow.</p>
<div class="highlight-console"><div class="highlight"><pre><span></span><span class="gp">$</span> ctags ... -D IGNORE_THIS ...
</pre></div>
</div>
<p>With this commandline the following C/C++ input</p>
<div class="highlight-C"><div class="highlight"><pre><span></span><span class="kt">int</span> <span class="n">IGNORE_THIS</span> <span class="n">a</span><span class="p">;</span>
</pre></div>
</div>
<p>will be processed as if it was</p>
<div class="highlight-C"><div class="highlight"><pre><span></span><span class="kt">int</span> <span class="n">a</span><span class="p">;</span>
</pre></div>
</div>
<p>Defining a macro with parameters uses the following syntax:</p>
<div class="highlight-console"><div class="highlight"><pre><span></span><span class="gp">$</span> ctags ... -D <span class="s2">&quot;foreach(arg)=for(arg;;)&quot;</span> ...
</pre></div>
</div>
<p>This example defines <code class="docutils literal"><span class="pre">for(arg;;)</span></code> as the replacement <code class="docutils literal"><span class="pre">foreach(arg)</span></code>.
So the following C/C++ input</p>
<div class="highlight-C"><div class="highlight"><pre><span></span><span class="n">foreach</span><span class="p">(</span><span class="kt">char</span> <span class="o">*</span> <span class="n">p</span><span class="p">,</span><span class="n">pointers</span><span class="p">)</span>
<span class="p">{</span>

<span class="p">}</span>
</pre></div>
</div>
<p>is processed in new C/C++ parser as:</p>
<div class="highlight-C"><div class="highlight"><pre><span></span><span class="k">for</span><span class="p">(</span><span class="kt">char</span> <span class="o">*</span> <span class="n">p</span><span class="p">;;)</span>
<span class="p">{</span>

<span class="p">}</span>
</pre></div>
</div>
<p>and the p local variable can be extracted.</p>
<p>The previous commandline includes quotes since the macros generally contain
characters that are treated specially by the shells. You may need some escaping.</p>
<p>Token pasting is performed by the <code class="docutils literal"><span class="pre">##</span></code> operator, just like in the normal
C preprocessor.</p>
<div class="highlight-console"><div class="highlight"><pre><span></span><span class="gp">$</span> ctags ... -D <span class="s2">&quot;DECLARE_FUNCTION(prefix)=int prefix ## Call();&quot;</span>
</pre></div>
</div>
<p>So the following code</p>
<div class="highlight-C"><div class="highlight"><pre><span></span><span class="n">DECLARE_FUNCTION</span><span class="p">(</span><span class="n">a</span><span class="p">)</span>
<span class="n">DECLARE_FUNCTION</span><span class="p">(</span><span class="n">b</span><span class="p">)</span>
</pre></div>
</div>
<p>will be processed as</p>
<div class="highlight-C"><div class="highlight"><pre><span></span><span class="kt">int</span> <span class="nf">aCall</span><span class="p">();</span>
<span class="kt">int</span> <span class="nf">bCall</span><span class="p">();</span>
</pre></div>
</div>
<p>Macros with variable arguments use the gcc <code class="docutils literal"><span class="pre">__VA_ARGS__</span></code> syntax.</p>
<div class="highlight-console"><div class="highlight"><pre><span></span><span class="gp">$</span> ctags ... -D <span class="s2">&quot;DECLARE_FUNCTION(name,...)=int name(__VA_ARGS__);&quot;</span>
</pre></div>
</div>
<p>So the following code</p>
<div class="highlight-C"><div class="highlight"><pre><span></span><span class="n">DECLARE_FUNCTION</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="kt">int</span> <span class="n">a</span><span class="p">,</span><span class="kt">int</span> <span class="n">b</span><span class="p">)</span>
</pre></div>
</div>
<p>will be processed as</p>
<div class="highlight-C"><div class="highlight"><pre><span></span><span class="kt">int</span> <span class="nf">x</span><span class="p">(</span><span class="kt">int</span> <span class="n">a</span><span class="p">,</span><span class="kt">int</span> <span class="n">b</span><span class="p">);</span>
</pre></div>
</div>
</div>
</div>
<div class="section" id="incompatible-changes">
<h2>Incompatible Changes<a class="headerlink" href="#incompatible-changes" title="Permalink to this headline">¶</a></h2>
<p>The parser is mostly compatible with the old one. There are some minor
incompatible changes which are described below.</p>
<div class="section" id="anonymous-structure-names">
<h3>Anonymous structure names<a class="headerlink" href="#anonymous-structure-names" title="Permalink to this headline">¶</a></h3>
<p>The old parser produced structure names in the form <code class="docutils literal"><span class="pre">__anonN</span></code> where N
was a number starting at 1 in each file and increasing at each new
structure. This caused collisions in symbol names when ctags was run
on multiple files.</p>
<p>In the new parser the anonymous structure names depend on the file name
being processed and on the type of the structure itself. Collisions are
far less likely (though not impossible as hash functions are unavoidably
imperfect).</p>
<p>Pitfall: the file name used for hashing includes the path as passed to the
ctags executable. So the same file “seen” from different paths will produce
different structure names. This is unavoidable and is up to the user to
ensure that multiple ctags runs are started from a common directory root.</p>
</div>
<div class="section" id="file-scope">
<h3>File scope<a class="headerlink" href="#file-scope" title="Permalink to this headline">¶</a></h3>
<p>The file scope information is not 100% reliable. It never was.
There are several cases in that compiler, linker or even source code
tricks can “unhide” file scope symbols (for instance *.c files can be
included into each other) and several other cases in that the limitation
of the scope of a symbol to a single file simply cannot be determined
with a single pass or without looking at a program as a whole.</p>
<p>The new parser defines a simple policy for file scope association
that tries to be as compatible as possible with the old parser and
should reflect the most common usages. The policy is the following:</p>
<ul class="simple">
<li>Namespaces are in file scope if declared inside a .c or .cpp file</li>
<li>Function prototypes are in file scope if declared inside a .c or .cpp file</li>
<li>K&amp;R style function definitions are in file scope if declared static
inside a .c file.</li>
<li>Function definitions appearing inside a namespace are in file scope only
if declared static inside a .c or .cpp file.
Note that this rule includes both global functions (global namespace)
and class/struct/union members defined outside of the class/struct/union
declaration.</li>
<li>Function definitions appearing inside a class/struct/union declaration
are in file scope only if declared static inside a .cpp file</li>
<li>Function parameters are always in file scope</li>
<li>Local variables are always in file scope</li>
<li>Variables appearing inside a namespace are in file scope only if
they are declared static inside a .c or .cpp file</li>
<li>Variables that are members of a class/struct/union are in file scope
only if declared in a .c or .cpp file</li>
<li>Typedefs are in file scope if appearing inside a .c or .cpp file</li>
</ul>
<p>Most of these rules are debatable in one way or the other. Just keep in mind
that this is not 100% reliable.</p>
</div>
<div class="section" id="inheritance-information">
<h3>Inheritance information<a class="headerlink" href="#inheritance-information" title="Permalink to this headline">¶</a></h3>
<p>The new parser does not strip template names from base classes.
For a declaration like</p>
<div class="highlight-C"><div class="highlight"><pre><span></span><span class="n">template</span><span class="o">&lt;</span><span class="kr">typename</span> <span class="n">A</span><span class="o">&gt;</span> <span class="n">class</span> <span class="nl">B</span> <span class="p">:</span> <span class="n">public</span> <span class="n">C</span><span class="o">&lt;</span><span class="n">A</span><span class="o">&gt;</span>
</pre></div>
</div>
<p>the old parser reported <code class="docutils literal"><span class="pre">C</span></code> as base class while the new one reports
<code class="docutils literal"><span class="pre">C&lt;A&gt;</span></code>.</p>
</div>
<div class="section" id="typeref">
<h3>Typeref<a class="headerlink" href="#typeref" title="Permalink to this headline">¶</a></h3>
<p>The syntax of the typeref field (<code class="docutils literal"><span class="pre">typeref:A:B</span></code>) was designed with only
struct/class/union/enum types in mind. Generic types don’t have <code class="docutils literal"><span class="pre">A</span></code>
information and the keywords became entirely optional in C++:
you just can’t tell. Furthermore, struct/class/union/enum types
share the same namespace and their names can’t collide, so the <code class="docutils literal"><span class="pre">A</span></code>
information is redundant for most purposes.</p>
<p>To accommodate generic types and preserve some degree of backward
compatibility the new parser uses struct/class/union/enum in place
of <code class="docutils literal"><span class="pre">A</span></code> where such keyword can be inferred. Where the information is
not available it uses the ‘typename’ keyword.</p>
<p>Generally, you should ignore the information in field <code class="docutils literal"><span class="pre">A</span></code> and use
only information in field <code class="docutils literal"><span class="pre">B</span></code>.</p>
</div>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h3><a href="index.html">Table Of Contents</a></h3>
  <ul>
<li><a class="reference internal" href="#">The new C/C++ parser</a><ul>
<li><a class="reference internal" href="#introduction">Introduction</a></li>
<li><a class="reference internal" href="#notable-new-features">Notable New Features</a><ul>
<li><a class="reference internal" href="#properties">Properties</a></li>
<li><a class="reference internal" href="#preprocessor-macros">Preprocessor macros</a></li>
</ul>
</li>
<li><a class="reference internal" href="#incompatible-changes">Incompatible Changes</a><ul>
<li><a class="reference internal" href="#anonymous-structure-names">Anonymous structure names</a></li>
<li><a class="reference internal" href="#file-scope">File scope</a></li>
<li><a class="reference internal" href="#inheritance-information">Inheritance information</a></li>
<li><a class="reference internal" href="#typeref">Typeref</a></li>
</ul>
</li>
</ul>
</li>
</ul>

  <h4>Previous topic</h4>
  <p class="topless"><a href="parsers.html"
                        title="previous chapter">Parsers</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="parser-vim.html"
                        title="next chapter">The Vim parser</a></p>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <form class="search" action="search.html" method="get">
      <div><input type="text" name="q" /></div>
      <div><input type="submit" value="Go" /></div>
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="parser-vim.html" title="The Vim parser"
             >next</a> |</li>
        <li class="right" >
          <a href="parsers.html" title="Parsers"
             >previous</a> |</li>
        <li class="nav-item nav-item-0"><a href="index.html">Universal Ctags 0.3.0 documentation</a> &#187;</li>
          <li class="nav-item nav-item-1"><a href="parsers.html" >Parsers</a> &#187;</li> 
      </ul>
    </div>
    <div class="footer" role="contentinfo">
        &#169; Copyright 2015, Universal Ctags Team.
      Last updated on 19 Dec 2017.
      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.6.4.
    </div>
  </body>
</html>